package com.monkey.core.plugin.web.validator;

import com.monkey.core.plugin.dto.MonkeyDto;

import java.math.BigDecimal;

/**
 * Project  : monkey
 * Author   : Wu Tian Qiang
 * Date     : 2016/10/8
 */
public enum ConstraintType {
    AssertFalse {
        @Override
        public void validate(MonkeyDto monkeyDto, ParameterConstraint constraint) {
            ValidationUtils.assertFalse(monkeyDto.getBoolean(constraint.name()), constraint.assertFalse().message());
        }
    },
    AssertTrue {
        @Override
        public void validate(MonkeyDto monkeyDto, ParameterConstraint constraint) {
            ValidationUtils.assertTrue(monkeyDto.getBoolean(constraint.name()), constraint.assertTrue().message());
        }
    },
    DecimalMax {
        @Override
        public void validate(MonkeyDto monkeyDto, ParameterConstraint constraint) {
            javax.validation.constraints.DecimalMax decimalMax = constraint.decimalMax();
            ValidationUtils.decimalMax(new BigDecimal(decimalMax.value()), monkeyDto.getString(constraint.name()), decimalMax.inclusive(), decimalMax.message());
        }
    },
    DecimalMin {
        @Override
        public void validate(MonkeyDto monkeyDto, ParameterConstraint constraint) {
            javax.validation.constraints.DecimalMin decimalMin = constraint.decimalMin();
            ValidationUtils.decimalMin(new BigDecimal(decimalMin.value()), monkeyDto.getString(constraint.name()), decimalMin.inclusive(), decimalMin.message());
        }
    },
    Digits {
        @Override
        public void validate(MonkeyDto monkeyDto, ParameterConstraint constraint) {
            javax.validation.constraints.Digits digits = constraint.digits();
            ValidationUtils.digits(digits.integer(), digits.fraction(), monkeyDto.getString(constraint.name()), digits.message());
        }
    },
    Future {
        @Override
        public void validate(MonkeyDto monkeyDto, ParameterConstraint constraint) {
            javax.validation.constraints.Future future = constraint.future();
            ValidationUtils.future(monkeyDto.getDate(constraint.name()), future.message());
        }
    }, //判断一个将来的时间
    Max {
        @Override
        public void validate(MonkeyDto monkeyDto, ParameterConstraint constraint) {
            javax.validation.constraints.Max max = constraint.max();
            ValidationUtils.max(new BigDecimal(max.value()), monkeyDto.getString(constraint.name()), max.message());
        }
    },
    Min {
        @Override
        public void validate(MonkeyDto monkeyDto, ParameterConstraint constraint) {
            javax.validation.constraints.Min min = constraint.min();
            ValidationUtils.min(new BigDecimal(min.value()), monkeyDto.getString(constraint.name()), min.message());
        }
    },
    NotNull {
        @Override
        public void validate(MonkeyDto monkeyDto, ParameterConstraint constraint) {
            javax.validation.constraints.NotNull notNull = constraint.notNull();
            ValidationUtils.notNull(monkeyDto.get(constraint.name()), notNull.message());
        }
    },
    Null {
        @Override
        public void validate(MonkeyDto monkeyDto, ParameterConstraint constraint) {
            javax.validation.constraints.Null aNull = constraint.null_();
            ValidationUtils.null_(monkeyDto.get(constraint.name()), aNull.message());
        }
    },
    Past {
        @Override
        public void validate(MonkeyDto monkeyDto, ParameterConstraint constraint) {
            javax.validation.constraints.Past past = constraint.past();
            ValidationUtils.past(monkeyDto.getDate(constraint.name()), past.message());
        }
    }, //判断一个过去时间
    Pattern {
        @Override
        public void validate(MonkeyDto monkeyDto, ParameterConstraint constraint) {
            javax.validation.constraints.Pattern pattern = constraint.pattern();
            ValidationUtils.pattern(pattern.regexp(), pattern.flags(), monkeyDto.getString(constraint.name()), pattern.message());
        }
    },
    Email {
        @Override
        public void validate(MonkeyDto monkeyDto, ParameterConstraint constraint) {
            org.hibernate.validator.constraints.Email email = constraint.email();
            ValidationUtils.email(monkeyDto.getString(constraint.name()), email.message());
        }
    },
    Length {
        @Override
        public void validate(MonkeyDto monkeyDto, ParameterConstraint constraint) {
            org.hibernate.validator.constraints.Length length = constraint.length();
            ValidationUtils.length(length.min(), length.max(), monkeyDto.getString(constraint.name()), length.message());
        }
    },
    NotBlank {
        @Override
        public void validate(MonkeyDto monkeyDto, ParameterConstraint constraint) {
            org.hibernate.validator.constraints.NotBlank notBlank = constraint.notBlank();
            ValidationUtils.notBlank(monkeyDto.getString(constraint.name()), notBlank.message());
        }
    },
    Range {
        @Override
        public void validate(MonkeyDto monkeyDto, ParameterConstraint constraint) {
            org.hibernate.validator.constraints.Range range = constraint.range();
            ValidationUtils.range(range.min(), range.max(), monkeyDto.getString(constraint.name()), range.message());
        }
    };

    public abstract void validate(MonkeyDto monkeyDto, ParameterConstraint constraint);
}
